import 'dart:io';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:photo_manager/photo_manager.dart';
import 'package:vischat/data/models/panel_item.dart';
import 'package:vischat/generated/icons/app_icons.dart';
import 'package:vischat/generated/route_table.dart';
import 'package:vischat/modules/home/socialize/chat/message_send_controller.dart';

class MessageOptionsController extends GetxController {
  final MessageSendController _sendController;
  MessageOptionsController(this._sendController);

  final _pageController = PageController();

  final _page1Items = <PanelItem>[].obs;
  final _currentPage = 0.obs;

  get pageController => _pageController;
  get page1Items => _page1Items;
  get currentPage => _currentPage;

  @override
  void onInit() {
    super.onInit();
    _page1Items.value = [
      PanelItem(
        icon: AppIcons.photo,
        label: 'morePanel.photo'.tr,
        onTap: () => _sendAsset(),
      ),
      PanelItem(
        icon: AppIcons.camera,
        label: 'morePanel.camera'.tr,
        onTap: () {},
      ),
      PanelItem(
        icon: AppIcons.location,
        label: 'morePanel.location'.tr,
        onTap: () {},
      ),
      PanelItem(
        icon: AppIcons.folder,
        label: 'morePanel.file'.tr,
        onTap: () {},
      ),
    ];
  }

  void _sendAsset() async {
    final result = await Get.toNamed(
      RouteTable.MEDIA_PICKER,
      arguments: {"maxSelection": 1},
    );
    final List<AssetEntity>? selectedMedia =
        result is List<AssetEntity> ? result : null;
    if (selectedMedia == null || selectedMedia.isEmpty) return;

    for (AssetEntity assetEntity in selectedMedia) {
      final File? file = await assetEntity.file;
      if (file == null) {
        continue;
      }
      switch (assetEntity.type) {
        case AssetType.image:
          _sendController.sendImage(file.path);
          break;
        case AssetType.video:
          break;
        case AssetType.audio:
          break;
        case AssetType.other:
          break;
      }
    }
  }

  void changePage(int index) {
    _currentPage.value = index;
  }
}
